<?php

class Pages_model extends Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::Model();
			
		$this->_table = $this->config->item('database_tables');
	}

	// Public methods
	public function get_pages()
	{
		$this->db->select('*');
		$this->db->where('type','p');
		if($lang_id)
		{
			$this->db->where('lang_id',$lang_id);
		}
		$this->db->order_by('id', 'DESC');
			
		$query = $this->db->get($this->_table['news'] . ' news');
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();

			foreach (array_keys($result) as $key)
			{
				$result[$key]['categories'] = '';
			}
			return $result;
		}
	}

	public function get_page($id)
	{
		$this->db->select('*');
		$this->db->where('type','p');
		$this->db->where('id', $id);
			
		$query = $this->db->get($this->_table['news'], 1);
			
		if ($query->num_rows() == 1)
		{
			return $query->row_array();
		}
	}

	public function create_page($lang_id)
	{
		//echo 'hhh';
		$create_date = ($this->input->post('publish_date') != '') ? date('Y-m-d', strtotime($this->input->post('publish_date'))) : date('Y-m-d');
		
		$sticky = ($this->input->post('sticky') == 1) ? '1' : '0';	
		$data = array
					(
						'creator_id' => $this->session->userdata('user_id'),
						'created_date' => $create_date,
						'expired_date' => $create_date,
						'publiced_date' => $create_date,
						'modified_date' => $create_date,
						'views' => 0,
						'order'=> 0,						
						'lang_id'=>($lang_id)?$lang_id:2,
						'title' => $this->input->post('title'),
						'url_title' => url_title($this->input->post('title'), 'dash', TRUE),
						'excerpt' => html_entity_decode($this->input->post('fck_p_excerpt'), ENT_NOQUOTES, 'UTF-8'),
						'content' => html_entity_decode($this->input->post('fck_p_content'), ENT_NOQUOTES, 'UTF-8'),
						//'allow_comments' => $allow_comments,
						'sticky' => $sticky,
						'status' => $this->input->post('status'),
						'type'=>'p'
					);	
		$this->db->insert($this->_table['news'], $data);
	}

	public function edit_page()
	{
		$created_date = ($this->input->post('publish_date') != '') ? date('Y-m-d', strtotime($this->input->post('publish_date'))) : date('Y-m-d');
		
		$sticky = ($this->input->post('sticky') == 1) ? '1' : '0';
			
		$data = array
					(
						'creator_id' => $this->session->userdata('user_id'),
						'created_date' => $created_date,
						'title' => $this->input->post('title'),
						'url_title' => url_title($this->input->post('title'), 'dash', TRUE),
						'excerpt' => html_entity_decode($this->input->post('fck_p_excerpt'), ENT_NOQUOTES, 'UTF-8'),
						'content' => html_entity_decode($this->input->post('fck_p_content'), ENT_NOQUOTES, 'UTF-8'),
						//'allow_comments' => $allow_comments,
						'sticky' => $sticky,
						'status' => $this->input->post('status'),
						'type'=>'p'
					);
			
		$this->db->where('id', $this->input->post('id'));
		$this->db->update($this->_table['news'], $data);
	}

	public function delete_page($id)
	{
		$this->db->where('id', $id);
			
		$this->db->delete($this->_table['news']);
	}
}

/* End of file pages_model.php */
/* Location: ./application/modules/admin/models/pages_model.php */